"use strict"

const Controller = require('egg').Controller

class MainController extends Controller {

    //验证登录是否成功
    async checklogin() {
        let username = this.ctx.request.body.username;
        let password = this.ctx.request.body.password;
        const sql = " SELECT username FROM admin_user WHERE username = '" + username +
            "' AND password = '" + password + "'"
        const res = await this.app.mysql.query(sql)

        if (res.length > 0) {
            let openId = new Date().getTime() //记录时间戳做openId
            this.ctx.session.openId = { "openId": openId }
            this.ctx.body = { 'data': '成功登录', 'openId': openId }
            console.log(this.ctx.session.openId);
        }
        else {
            this.ctx.body = { 'data': '登录失败' }
        }
    }

    // 添加发布文章
    async addArticle() {
        let tmpArticle = this.ctx.request.body
        // tmpArticle.
        console.log(tmpArticle, 'tmpArticle');
        console.log(this.app.mysql.insert, 'mysql');
        const result = await this.app.mysql.insert('article', tmpArticle)
        const insertSuccess = result.affectedRows === 1
        const insertId = this.ctx.request.body.id

        this.ctx.body = {
            insertSuccess: insertSuccess,
            insertId: insertId
        }
    }
    //修改文章
    async updateArticle() {
        let tmpArticle = this.ctx.request.body;
        const result = await this.app.mysql.update('article', tmpArticle)
        console.log(result.affectedRows);
        const isUpdateScuccess = result.affectedRows === 1
        const insertId = this.ctx.request.body.id;
        this.ctx.body = {
            isUpdateScuccess,
            insertId
        }
    }
    //保存文章
    async saveArticle() {
        let tmpArticle = this.ctx.request.body;
        const result = await this.app.mysql.insert('save_article', tmpArticle)
        const insertSuccess = result.affectedRows === 1
        const insertId = this.ctx.request.body.id;
        this.ctx.body = {
            insertSuccess,
            insertId
        }
    }
    //修改保存文章
    async updateSaveArticle() {
        let tmpArticle = this.ctx.request.body;
        const result = await this.app.mysql.update('save_article', tmpArticle)
        const isUpdateScuccess = result.affectedRows === 1
        const insertId = this.ctx.request.body.id;
        this.ctx.body = {
            isUpdateScuccess,
            insertId
        }
    }
    //获取文章列表
    async getArticleList() {
        // let time = 'select add_time from article';
        // let newtime = time.mktime(time.strptime(time,"%Y-%m-%d %H:%M:%S"))
        let sql = 'SELECT article.id as id,'+
             'article.title as title,'+
             'article.introduce as introduce,'+
             'article.add_time as addTime,'+
             'article.view_count as view_count ,'+
             'type.type_name as typeName '+
             'FROM article LEFT JOIN type ON article.type_id = type.id'
            //  'ORDER BY newtime DESC'

        const results = await this.app.mysql.query(sql)
        // this.ctx.body = await this.app.mysql.query("select * from article")
        this.ctx.body = {
            data: results
        }
    }
    // 获取草稿文章
    async getSaveArticle (){
        let sql = 'select * from save_article'
        const results = await this.app.mysql.query(sql)
        this.ctx.body = {
            data: results
        }
    }
    //删除文章
    async deleteArticle(){
        let id = this.ctx.params.id;
        console.log(id,'id');
        const res = await this.app.mysql.delete("article",{'id':id})
        console.log(res,'reds');
        this.tx.body = {data:res}
    }
    // 编辑文章
    async editArtcile (){
        let id  = this.ctx.params.id;
        let sql = 'select * from article where id = ' + id;
        const result = await this.app.mysql.query(sql)
        this.ctx.body={data:result}
    }
    //删除草稿箱
    async deleteDrafts(){
        let id = this.ctx.params.id;
        console.log(id,'id');
        const res = await this.app.mysql.delete("save_article",{'id':id})
        console.log(res,'reds');
        this.tx.body = {data:res}
    }
    //编辑草稿文章
    async editDrafts(){
        let id  = this.ctx.params.id;
        let sql = 'select * from save_article where id = ' + id;
        const result = await this.app.mysql.query(sql)
        console.log(result,'result');
        this.ctx.body={data:result}
    }
}

module.exports = MainController